package afcp.alumni.action.page;

import java.util.LinkedList;
import java.util.List;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import afcp.alumni.action.base.PageAction;
import afcp.alumni.model.db.Defect;
import afcp.alumni.service.impl.DefectService;
import afcp.alumni.service.impl.SF;

@UrlBinding(BugListAction.URL)
public class BugListAction extends PageAction {
	
	private DefectService defectService = SF.defect();

//	@Override
//	@Before
//	protected void setPageVisitedBoolean() {
//	}
	
//	private static final String TITLE = "Anomalies du site";

	@Override
	public String getTitle() {
		return msg("buglist.title");
	}

	private static final String JSP = "/WEB-INF/jsp/buglist.jsp";
	@Override
	protected String defaultJsp() {
		return JSP;
	}

	public static final String URL = "/buglist";
	@Override
	public String getUrl() {
		return URL;
	}

	@Override
	@DefaultHandler
	public Resolution view() {
		return this.toJsp();
	}
	
	public List<Defect> getBugList() {
		return defectService.getDefects();
	}
	
	
	// Add defect form
	
	public List<Defect.Criticity> getCriticities() {
		List<Defect.Criticity> criticitiesList = new LinkedList<>();
		String[] criticities = Defect.getCriticities();
		for (int i=(criticities.length-1); i>=0; i--) {
			criticitiesList.add(new Defect.Criticity(i, criticities[i]));
		}
		return criticitiesList;
	}
	

	@ValidateNestedProperties({
			@Validate(field = "description", required = true, on = "addDefect") })
	private Defect defect;

	public Defect getDefect() {
		return defect;
	}

	public void setDefect(Defect defect) {
		this.defect = defect;
	}
	
	public Resolution addDefect() {
		defectService.addDefect(defect);
		return toJsp();
	}
	
	// End add defect form

}
